﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using DataAccess;
using Model;
using UserControl = System.Windows.Controls.UserControl;

namespace Project
{
    /// <summary>
    /// Interaction logic for ComplaintPhotos.xaml
    /// </summary>
    public partial class ComplaintPhotos : UserControl
    {
        public long ComplaintId { get; set; }
        public ComplaintPhotos(long compId, string compRegDate)
        {
            InitializeComponent();
            ComplaintId = compId;
            lblCaseNumber.Content = ComplaintId;
            lblRegistrationDate.Content = compRegDate;
           Thread myThread = new Thread(ThreadMethod);
            myThread.IsBackground = true;
            myThread.Start();
            UIWindowManager.SetWindowTitle(this,"Brigada De Protección a la Familia - Ver Album de Fotos");
            HideAddPhotoIfDirector();
        }

        private void HideAddPhotoIfDirector()
        {
            if (UserControllerAndDataAccess.IsUserLogged())
            {
                if (UserControllerAndDataAccess.IsDirectorLogged)
                {
                    lblAddPhotoInfo.Visibility = Visibility.Collapsed;
                    btnSaveNew.Visibility = Visibility.Collapsed;
                }
            }
        }

        public void ThreadMethod()
        {
            BindImages();
            this.Dispatcher.Invoke(new System.Action(() =>
                                                         {
                                                             Loader.Visibility = Visibility.Collapsed;
                                                             listGrid.Margin = new Thickness(0, 122, 0, 0);
                                                         }));
        }

        private void BindImages()
        {
            try
            {
                List<ImageEntity> ListImageObj = PhotoAlbumDataAccess.GetAllImagesData(ComplaintId);
                if (ListImageObj.Count > 0)
                {
                    LsImageGallery.Dispatcher.Invoke(new Action(() => {LsImageGallery.Items.Clear();}), DispatcherPriority.SystemIdle);
                    foreach (var imageEntity in ListImageObj)
                    {
                        LsImageGallery.Dispatcher.Invoke(new Action(() =>
                        {
                            LsImageGallery.Items.Add(imageEntity);
                        }), DispatcherPriority.SystemIdle);
                    }
                    
                } 
            }
            catch (DirectoryNotFoundException ex)
            {
                this.Dispatcher.Invoke(new System.Action(() =>
                {
                    lblMessage.Visibility = Visibility.Visible;
                }));
            }
        }

        private void btnSaveNew_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "JPG (*.jpg,*.jpeg)|*.jpg;*.jpeg";
            DialogResult dgRes = openDialog.ShowDialog();
            if (dgRes == DialogResult.OK)
            {
                try
                {
                    PhotoAlbumDataAccess.InsertImageToComplaint(ComplaintId, openDialog.FileName, openDialog.SafeFileName);
                    lblMessage.Visibility = Visibility.Collapsed;
                    Thread myThread = new Thread(ThreadMethod);
                    myThread.IsBackground = true;
                    myThread.Start();
                    Xceed.Wpf.Toolkit.MessageBox.Show("Se agregó la imagen exitosamente!","Exito!");
                }
                catch (IOException)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show("Ya se agregó previamente una imagen con el mismo nombre para la denuncia, no se pudo agregar la imagen solicitada", "Error");
                }
                catch (Exception)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show("Un error ocurrio mientras se guardaba la imagen", "Error");
                }
            }
        }

        private void LsImageGallery_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (LsImageGallery.SelectedValue != null)
            {
                var imag = LsImageGallery.SelectedValue as ImageEntity;
                System.Diagnostics.Process.Start(imag.ImagePath);
            }
        }
    }
}
